package client.comm.commlib.base

import android.content.Context
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.commlib.R
import client.comm.commlib.databinding.LayoutRcLoadmoreBinding


abstract class BaseLoadmoreAdapter<T>(
    context: Context, diffCallback: DiffUtil.ItemCallback<T> = object : DiffUtil.ItemCallback<T>() {
        override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
            return false
        }

        override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
            return false
        }
    }
) :
    BaseAdapter<T>(context, diffCallback) {

    val hasMoreState = MutableLiveData<Boolean>(true).apply {
        observe(context as LifecycleOwner, {
            notifyItemChanged(itemCount - 1)
        })
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 2)
            return BindingViewHoder(
                DataBindingUtil.inflate<ViewDataBinding>(
                    layoutInflater,
                    R.layout.layout_rc_loadmore, parent, false
                )
            ).apply {

                val lp: ViewGroup.LayoutParams = itemView.getLayoutParams()
                if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams) {
                    lp.isFullSpan = true
                }
            }


        val holder = createVh(parent, viewType)
        return holder
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder.itemViewType == 2) {
            val binding = (holder as BindingViewHoder<LayoutRcLoadmoreBinding>).binding
            binding.event = this
        } else {
            bindVh(holder, position)
        }
    }

    fun submitListHasHeader(list: List<T?>?) {
        list?.let {
            if (list.size > 0) {
                if (list.size > 1 && list.get(list.lastIndex) != null)
                    (list as MutableList<T?>).add(null)
                super.submitList(list)
            } else if (list.size == 0) {
                mItems.clear()
                notifyDataSetChanged()
            }
        }
    }

    override fun submitList(list: List<T?>?) {
        list?.let {
            if (list.size > 0 && list.get(list.lastIndex) != null) {
                (list as MutableList<T?>).add(null)
                super.submitList(list)
            } else if (list.size == 0) {
                mItems.clear()
                notifyDataSetChanged()
            }
        }
    }

    fun appendList(list: MutableList<T?>?) {
        list?.let {
            if (it.size > 0 && !list.contains(null)) {
                val index = itemCount - 1
                mItems.addAll(index, it)
                notifyItemRangeInserted(index, it.size)
            }
        }
    }

    abstract open fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder
    abstract open fun bindVh(holder: RecyclerView.ViewHolder, position: Int)

    override fun getItemViewType(position: Int): Int {
        if (itemCount > 0 && position == itemCount - 1)
            return 2

        return 0;
    }


}